<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<style>
    * {
        padding: 0;
        margin: 0;
    }

    body {
        height: 100vh;
        overflow: hidden;
    }
</style>
<body>

<script>
    //方法二
    var solveSudoku = function (board) {
        const rows = new Array(9)
        const cols = new Array(9)
        const blocks = new Array(9)
        const opitons = ['1', '2', '3', '4', '5', '6', '7', '8', '9']
        for (let i = 0; i < 9; i++) { //初始化
            rows[i] = new Set(opitons)
            cols[i] = new Set(opitons)
            blocks[i] = new Set(opitons)
        }

        const getBlockIndex = (i, j) => { //获取坐标所在框的索引
            return parseInt(i / 3) * 3 + parseInt(j / 3)
            // return (i / 3 | 0) * 3 + (j / 3 | 0)gi
        }

        for (let i = 0; i < 9; i++) { //更新set,删除该选项
            for (let j = 0; j < 9; j++) {
                if (board[i][j] != ".") {
                    rows[i].delete(board[i][j])
                    cols[j].delete(board[i][j])
                    blocks[getBlockIndex(i, j)].delete(board[i][j])
                }
            }
        }
        //递归
        const fill = (i, j) => {
            if (j == 9) { //换行
                i++
                j = 0
                if (i == 9) {
                    return true
                }
            }
            if (board[i][j] != '.') {
                return fill(i, j + 1)
            }
            const blockIndex = getBlockIndex(i, j)

            for (let num = 1; num <= 9; num++) {
                const s = String(num)
                if (!rows[i].has(s) || !cols[j].has(s) || !blocks[blockIndex].has(s)) { //如果数组中没有,continue
                    continue
                }
                board[i][j] = s
                rows[i].delete(board[i][j])
                cols[j].delete(board[i][j])
                blocks[blockIndex].delete(board[i][j])

                if (fill(i, j + 1)) {
                    return true
                }
                board[i][j] = "."
                rows[i].add(s)
                cols[j].add(s)
                blocks[blockIndex].add(s)
            }
            return false
        }
        fill(0, 0)
        return board
    }
    var board = [
        [".", ".", ".", "8", "1", "9", ".", "5", "."],
        [".", ".", "3", ".", ".", ".", ".", "8", "."],
        [".", ".", ".", ".", ".", "3", ".", ".", "6"],
        [".", ".", "8", "2", ".", ".", ".", "9", "5"],
        [".", "7", "9", ".", ".", ".", "4", "2", "."],
        ["5", "3", ".", ".", ".", "4", "6", ".", "."],
        ["7", ".", ".", "9", ".", ".", ".", ".", "."],
        [".", "8", ".", ".", ".", ".", "1", ".", "."],
        [".", "2", ".", "3", "4", "8", ".", ".", "."]
    ]

    // console.log(solveSudoku(board))


    function calc(i, j) {
        if (j == 9) { //换行
            i++
            j = 0
            if (i == 9) {
                return true
            }
        }
        if (board[i][j] != '.') {
            return calc(i, j + 1)
        }
        for (let num = 1; num <= 9; num++) {
            board[i][j] = (num + "");
            if (check(i, j)) {
                if (calc(i, j + 1)) {
                    return true
                }
                board[i][j] = ".";
            }

        }
        return false
    }

    function check(i, j) {
        return checkRow(i, j) && checkColumn(i, j) && checkBlock(i, j);
    }

    function checkRow(i, j) {
        let set = new Set();
        for (let k = 0; k < 9; k++) {
            if (board[i][k] == ".") {
                continue;
            }
            if (set.has(board[i][k])) {
                return false
            }
            set.add(board[i][k])
        }
        return true
    }

    function checkColumn(i, j) {
        let set = new Set();
        for (let k = 0; k < 9; k++) {
            if (board[k][j] == ".") {
                continue;
            }
            if (set.has(board[k][j])) {
                return false
            }
            set.add(board[k][j])
        }
        return true
    }

    function checkBlock(i, j) {
        let indexRow = parseInt(i / 3);
        let indexColumn = parseInt(j / 3);
        let set = new Set();
        let arr = [board[indexRow * 3 + 0][indexColumn * 3 + 0], board[indexRow * 3 + 0][indexColumn * 3 + 1], board[indexRow * 3 + 0][indexColumn * 3 + 2],
            board[indexRow * 3 + 1][indexColumn * 3 + 0], board[indexRow * 3 + 1][indexColumn * 3 + 1], board[indexRow * 3 + 1][indexColumn * 3 + 2],
            board[indexRow * 3 + 2][indexColumn * 3 + 0], board[indexRow * 3 + 2][indexColumn * 3 + 1], board[indexRow * 3 + 2][indexColumn * 3 + 2],
        ]
        console.log(arr)
        for (let k = 0; k < 9; k++) {
            if (arr[k] == ".") {
                continue;
            }
            if (set.has(arr[k])) {
                return false
            }
            set.add(arr[k])
        }
        return true
    }

    // calc(0, 0)
    // console.log(board)
    window.addEventListener("load", function () {
        console.log("脚本启动")
        let apps = document.querySelectorAll("div.app-item-icon[title]")
        for (let i = 0; i < apps.length; i++) {
            if (apps[i].getAttribute("title").indexOf('Tool') === 0) {
                console.log(
                    apps[i].onclick
                )
                apps[i].onclick = null
                apps[i].onmouseup=null
                console.log(apps[i].title, "事件已经移除")
            }
        }
    })

</script>
</body>
</html>